<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */

class Selling extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->issetSession('Selling');
        $this->load->model('SellingModel');
    }
    
    public function index()
    {
        $content_array = $this->SellingModel->getSelling(base_url()."selling/index/",PER_PAGE_CRM);
		
        foreach($content_array as $key=>$value)
        {
            $content_array[$key]['pricearr'] = 0;
            foreach($content_array[$key]['price'] as $k=>$v){
               $content_array[$key]['pricearr'] += (float)@$content_array[$key]['count'][$k]*(float)@$content_array[$key]['price'][$k]; 
            }
        }
        $pages = $this->pagination->create_links();
        $this->crud->assign('selling' ,$content_array)
                ->assign('pages', $pages)->display('selling.tpl');
    }
    
    public function statusupdate()
    {
        $r = $this->input->post();
        /*if($r['status'] == 'Отменено')
        {
            $order = $this->SellingModel->get_one('tb_order', 'id', $r['id']); 
            $stor = $this->SellingModel->get_one('tb_product', 'id',$order[0]['id_product']);
            (int)$stor[0]['reserved'] -=(int)$order[0]['count'];
            $this->SellingModel->update_post('tb_product', 'id', $order[0]['id_product'], array('reserved'=>(string)$stor[0]['reserved']));
        }*/
        
        $resp = $this->SellingModel->update_post('tb_order', 'id', $r['id'], array('status'=>$r['status'],'date_ship'=>$r['date_ship']));
        if($resp =="1")
        {
            die('Заказ по коду '.$r['id'].' поменял данные ');
        }
        die('Невозможно поменять статус');
    }
    
    public function find()
    {
       $p = $this->input->get();
       if(empty($p['status']) && empty($p['name']) )
       {
           redirect(site_url("selling/"), 'refresh');
       }
       
       $content_array = $this->SellingModel->Filter(base_url()."selling/find/", $p, PER_PAGE_CRM);
       if(isset($content_array['error']))
       {
            $this->crud->assign('error' ,$content_array)->display('selling.tpl');
       }
       else
       {
            foreach($content_array as $key=>$value)
            {
                $content_array[$key]['pricearr'] = 0;
                foreach($content_array[$key]['price'] as $k=>$v){
                   $content_array[$key]['pricearr'] += (float)@$content_array[$key]['count'][$k]*(float)@$content_array[$key]['price'][$k]; 
                }
            }
            $pages = $this->pagination->create_links();
            $this->crud->assign('selling' ,$content_array)
                ->assign('pages', $pages)->display('selling.tpl');
       }
       
    }
}